{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 示例选择器\n",
    "- 根据长度要求智能选择\n",
    "- 根据输入相似度选择示例(最大边际相关性)\n",
    "- 根据输入响度选择示例(最大余弦相似度)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8bc3203ea9bce684"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 根据长度要求智能选择"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "26078266ab821046"
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "# 根据输入的提示词长度综合计算最终长度，智能截取或者添加提示词示例\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.prompts import FewShotPromptTemplate\n",
    "from langchain.prompts.example_selector import LengthBasedExampleSelector\n",
    "\n",
    "\n",
    "# 假设已经有非常多的提示词示例\n",
    "examples = [\n",
    "    {\"input\":\"happy\", \"output\":\"sad\"},\n",
    "    {\"input\":\"tall\", \"output\":\"short\"},\n",
    "    {\"input\":\"sunny\", \"output\":\"gloom\"},\n",
    "    {\"input\":\"windy\", \"output\":\"calm\"},\n",
    "    {\"input\":\"高兴\", \"output\":\"悲伤\"}\n",
    "]\n",
    "\n",
    "# 构造提示词模板\n",
    "example_prompt = PromptTemplate(\n",
    "    input_variables = [\"input\", \"output\"],\n",
    "    template = \"原词: {input}\\n反义词: {output}\"\n",
    ")\n",
    "\n",
    "# 调用示例选择器- 调用长度示例选择器\n",
    "example_selector = LengthBasedExampleSelector(\n",
    "    # 传入提示词示例组\n",
    "    examples = examples,\n",
    "    # 传入提示词模板\n",
    "    example_prompt = example_prompt,\n",
    "    # 设置格式化后的提示词最大长度\n",
    "    max_length = 25,\n",
    "    # 内置的get_text_length,如果默认粉刺计算方式不满足，可以自定义\n",
    "    # get_text_length :Callable[[str], int] = lambda x: len(re.split(\"\\n| \", x))\n",
    ")\n",
    "\n",
    "dynamic_examples = FewShotPromptTemplate(\n",
    "    example_selector = example_selector,\n",
    "    example_prompt = example_prompt,\n",
    "    # 模板解析前缀\n",
    "    prefix = \"给出每个输入此的反义词\",\n",
    "    # 模板解析后缀\n",
    "    suffix = \"原词:{adjective}\\n反义词:\",\n",
    "    input_variables = [\"adjective\"]\n",
    ")\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:43.390355600Z",
     "start_time": "2024-05-07T04:09:43.383156300Z"
    }
   },
   "id": "e48e35bc00b8f954"
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "给出每个输入此的反义词\n",
      "\n",
      "原词: happy\n",
      "反义词: sad\n",
      "\n",
      "原词: tall\n",
      "反义词: short\n",
      "\n",
      "原词: sunny\n",
      "反义词: gloom\n",
      "\n",
      "原词: windy\n",
      "反义词: calm\n",
      "\n",
      "原词: 高兴\n",
      "反义词: 悲伤\n",
      "\n",
      "原词:big\n",
      "反义词:\n"
     ]
    }
   ],
   "source": [
    "# 小样本获得所有示例\n",
    "print(dynamic_examples.format(adjective=\"big\"))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:44.190981600Z",
     "start_time": "2024-05-07T04:09:44.185471100Z"
    }
   },
   "id": "7573096a1a49807b"
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "给出每个输入此的反义词\n",
      "\n",
      "原词: happy\n",
      "反义词: sad\n",
      "\n",
      "原词: tall\n",
      "反义词: short\n",
      "\n",
      "原词: sunny\n",
      "反义词: gloom\n",
      "\n",
      "原词:big and huge and massive and large and gigantic and tall and enormous\n",
      "反义词:\n"
     ]
    }
   ],
   "source": [
    "# 如果输入长度很长，则最终输出会根据长度要求减少  （ps:我们设置的模板最大长度为25）\n",
    "long_string = \"big and huge and massive and large and gigantic and tall and enormous\"\n",
    "\n",
    "print(dynamic_examples.format(adjective=long_string))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:44.974746Z",
     "start_time": "2024-05-07T04:09:44.967419400Z"
    }
   },
   "id": "8066db01179a5f44"
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:46.215489500Z",
     "start_time": "2024-05-07T04:09:46.203534700Z"
    }
   },
   "id": "7da62b16c6fcd5d7"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 根据输入相似度选择示例(最大边际相关性)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e4e5c205561299cb"
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "# 使用MMR来检索相关示例，以使示例尽量符合输入\n",
    "from langchain.prompts.example_selector import MaxMarginalRelevanceExampleSelector\n",
    "from langchain.vectorstores import FAISS\n",
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "from langchain.prompts import FewShotPromptTemplate\n",
    "\n",
    "import os \n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-zk2f3fc211ff0ad224ecd79efe339d0861d66379d4d11320\"\n",
    "os.environ[\"OPENAI_PROXY\"] = \"https://flag.smarttrot.com/v1/\"\n",
    "\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "# 假设已经有非常多的提示词示例\n",
    "examples = [\n",
    "    {\"input\":\"happy\", \"output\":\"sad\"},\n",
    "    {\"input\":\"tall\", \"output\":\"short\"},\n",
    "    {\"input\":\"sunny\", \"output\":\"gloom\"},\n",
    "    {\"input\":\"windy\", \"output\":\"calm\"},\n",
    "    {\"input\":\"高兴\", \"output\":\"悲伤\"}\n",
    "]\n",
    "\n",
    "\n",
    "# 构造提示词模板\n",
    "example_prompt = PromptTemplate(\n",
    "    input_variables = [\"input\", \"output\"],\n",
    "    template = \"原词: {input}\\n反义词: {output}\"\n",
    ")\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:47.436383100Z",
     "start_time": "2024-05-07T04:09:47.423335900Z"
    }
   },
   "id": "b77f643fde25a582"
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "^C\n",
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: tiktoken in c:\\users\\admin\\appdata\\roaming\\python\\python311\\site-packages (0.3.3)\n",
      "Requirement already satisfied: regex>=2022.1.18 in g:\\anaconda\\lib\\site-packages (from tiktoken) (2022.7.9)\n",
      "Requirement already satisfied: requests>=2.26.0 in g:\\anaconda\\lib\\site-packages (from tiktoken) (2.31.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in g:\\anaconda\\lib\\site-packages (from requests>=2.26.0->tiktoken) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in g:\\anaconda\\lib\\site-packages (from requests>=2.26.0->tiktoken) (3.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in g:\\anaconda\\lib\\site-packages (from requests>=2.26.0->tiktoken) (1.26.16)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in g:\\anaconda\\lib\\site-packages (from requests>=2.26.0->tiktoken) (2023.7.22)\n",
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: faiss-cpu in c:\\users\\admin\\appdata\\roaming\\python\\python311\\site-packages (1.8.0)\n",
      "Requirement already satisfied: tiktoken in c:\\users\\admin\\appdata\\roaming\\python\\python311\\site-packages (0.3.3)\n",
      "Requirement already satisfied: numpy in g:\\anaconda\\lib\\site-packages (from faiss-cpu) (1.24.3)\n",
      "Requirement already satisfied: regex>=2022.1.18 in g:\\anaconda\\lib\\site-packages (from tiktoken) (2022.7.9)\n",
      "Requirement already satisfied: requests>=2.26.0 in g:\\anaconda\\lib\\site-packages (from tiktoken) (2.31.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in g:\\anaconda\\lib\\site-packages (from requests>=2.26.0->tiktoken) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in g:\\anaconda\\lib\\site-packages (from requests>=2.26.0->tiktoken) (3.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in g:\\anaconda\\lib\\site-packages (from requests>=2.26.0->tiktoken) (1.26.16)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in g:\\anaconda\\lib\\site-packages (from requests>=2.26.0->tiktoken) (2023.7.22)\n"
     ]
    }
   ],
   "source": [
    "# # token化\n",
    "# !pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tiktoken\n",
    "# # 检索\n",
    "# !pip install faiss-cpu -i https://pypi.tuna.tsinghua.edu.cn/simple tiktoken"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:54.297338Z",
     "start_time": "2024-05-07T04:09:48.292540700Z"
    }
   },
   "id": "a0abdb78b2b33187"
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "partially initialized module 'pandas' has no attribute 'core' (most likely due to a circular import)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[16], line 4\u001B[0m\n\u001B[0;32m      1\u001B[0m example_selector \u001B[38;5;241m=\u001B[39m MaxMarginalRelevanceExampleSelector\u001B[38;5;241m.\u001B[39mfrom_examples(\n\u001B[0;32m      2\u001B[0m     examples,\n\u001B[0;32m      3\u001B[0m     \u001B[38;5;66;03m# 使用OpenAI的模型\u001B[39;00m\n\u001B[1;32m----> 4\u001B[0m     OpenAIEmbeddings(openai_api_key\u001B[38;5;241m=\u001B[39mapi_key, openai_api_base\u001B[38;5;241m=\u001B[39mapi_base),\n\u001B[0;32m      5\u001B[0m     \u001B[38;5;66;03m# 使用FAISS检索\u001B[39;00m\n\u001B[0;32m      6\u001B[0m     FAISS,\n\u001B[0;32m      7\u001B[0m     \u001B[38;5;66;03m# 结果条数\u001B[39;00m\n\u001B[0;32m      8\u001B[0m     k \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m2\u001B[39m,\n\u001B[0;32m      9\u001B[0m )\n\u001B[0;32m     11\u001B[0m \u001B[38;5;66;03m# 使用小样本模板\u001B[39;00m\n\u001B[0;32m     12\u001B[0m mmr_prompt \u001B[38;5;241m=\u001B[39m FewShotPromptTemplate(\n\u001B[0;32m     13\u001B[0m     example_selector \u001B[38;5;241m=\u001B[39m example_selector,\n\u001B[0;32m     14\u001B[0m     example_prompt \u001B[38;5;241m=\u001B[39m example_prompt,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m     17\u001B[0m     input_variables \u001B[38;5;241m=\u001B[39m [\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124madjective\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[0;32m     18\u001B[0m )\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\pydantic\\main.py:339\u001B[0m, in \u001B[0;36mpydantic.main.BaseModel.__init__\u001B[1;34m()\u001B[0m\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\pydantic\\main.py:1102\u001B[0m, in \u001B[0;36mpydantic.main.validate_model\u001B[1;34m()\u001B[0m\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\langchain\\embeddings\\openai.py:177\u001B[0m, in \u001B[0;36mOpenAIEmbeddings.validate_environment\u001B[1;34m(cls, values)\u001B[0m\n\u001B[0;32m    170\u001B[0m values[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mopenai_organization\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m get_from_dict_or_env(\n\u001B[0;32m    171\u001B[0m     values,\n\u001B[0;32m    172\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mopenai_organization\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m    173\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mOPENAI_ORGANIZATION\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m    174\u001B[0m     default\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m    175\u001B[0m )\n\u001B[0;32m    176\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[1;32m--> 177\u001B[0m     \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\n\u001B[0;32m    179\u001B[0m     values[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mclient\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m openai\u001B[38;5;241m.\u001B[39mEmbedding\n\u001B[0;32m    180\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mImportError\u001B[39;00m:\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\openai\\__init__.py:9\u001B[0m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mcontextvars\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m ContextVar\n\u001B[0;32m      7\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mtyping\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Optional, TYPE_CHECKING\n\u001B[1;32m----> 9\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m     10\u001B[0m     Audio,\n\u001B[0;32m     11\u001B[0m     ChatCompletion,\n\u001B[0;32m     12\u001B[0m     Completion,\n\u001B[0;32m     13\u001B[0m     Customer,\n\u001B[0;32m     14\u001B[0m     Edit,\n\u001B[0;32m     15\u001B[0m     Deployment,\n\u001B[0;32m     16\u001B[0m     Embedding,\n\u001B[0;32m     17\u001B[0m     Engine,\n\u001B[0;32m     18\u001B[0m     ErrorObject,\n\u001B[0;32m     19\u001B[0m     File,\n\u001B[0;32m     20\u001B[0m     FineTune,\n\u001B[0;32m     21\u001B[0m     Image,\n\u001B[0;32m     22\u001B[0m     Model,\n\u001B[0;32m     23\u001B[0m     Moderation,\n\u001B[0;32m     24\u001B[0m )\n\u001B[0;32m     25\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01merror\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m APIError, InvalidRequestError, OpenAIError\n\u001B[0;32m     27\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m TYPE_CHECKING:\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\openai\\api_resources\\__init__.py:7\u001B[0m\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mdeployment\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Deployment  \u001B[38;5;66;03m# noqa: F401\u001B[39;00m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01medit\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Edit  \u001B[38;5;66;03m# noqa: F401\u001B[39;00m\n\u001B[1;32m----> 7\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01membedding\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Embedding  \u001B[38;5;66;03m# noqa: F401\u001B[39;00m\n\u001B[0;32m      8\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mengine\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Engine  \u001B[38;5;66;03m# noqa: F401\u001B[39;00m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01merror_object\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m ErrorObject  \u001B[38;5;66;03m# noqa: F401\u001B[39;00m\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\openai\\api_resources\\embedding.py:7\u001B[0m\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m util\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mabstract\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mengine_api_resource\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m EngineAPIResource\n\u001B[1;32m----> 7\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mdatalib\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m numpy \u001B[38;5;28;01mas\u001B[39;00m np, assert_has_numpy\n\u001B[0;32m      8\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01merror\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m TryAgain\n\u001B[0;32m     11\u001B[0m \u001B[38;5;28;01mclass\u001B[39;00m \u001B[38;5;21;01mEmbedding\u001B[39;00m(EngineAPIResource):\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\openai\\datalib.py:22\u001B[0m\n\u001B[0;32m     19\u001B[0m     numpy \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[0;32m     21\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[1;32m---> 22\u001B[0m     \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m\n\u001B[0;32m     23\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mImportError\u001B[39;00m:\n\u001B[0;32m     24\u001B[0m     pandas \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\pandas\\__init__.py:138\u001B[0m\n\u001B[0;32m    120\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mcore\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mreshape\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m    121\u001B[0m     concat,\n\u001B[0;32m    122\u001B[0m     lreshape,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    134\u001B[0m     qcut,\n\u001B[0;32m    135\u001B[0m )\n\u001B[0;32m    137\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m api, arrays, errors, io, plotting, tseries\n\u001B[1;32m--> 138\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m testing  \u001B[38;5;66;03m# noqa:PDF015\u001B[39;00m\n\u001B[0;32m    139\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mutil\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m_print_versions\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m show_versions\n\u001B[0;32m    141\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mio\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m    142\u001B[0m     \u001B[38;5;66;03m# excel\u001B[39;00m\n\u001B[0;32m    143\u001B[0m     ExcelFile,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    171\u001B[0m     read_spss,\n\u001B[0;32m    172\u001B[0m )\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\pandas\\testing.py:6\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;124;03mPublic testing utility functions.\u001B[39;00m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m----> 6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m_testing\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m      7\u001B[0m     assert_extension_array_equal,\n\u001B[0;32m      8\u001B[0m     assert_frame_equal,\n\u001B[0;32m      9\u001B[0m     assert_index_equal,\n\u001B[0;32m     10\u001B[0m     assert_series_equal,\n\u001B[0;32m     11\u001B[0m )\n\u001B[0;32m     13\u001B[0m __all__ \u001B[38;5;241m=\u001B[39m [\n\u001B[0;32m     14\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124massert_extension_array_equal\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m     15\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124massert_frame_equal\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m     16\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124massert_series_equal\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m     17\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124massert_index_equal\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m     18\u001B[0m ]\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\pandas\\_testing\\__init__.py:903\u001B[0m\n\u001B[0;32m    898\u001B[0m     \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mpytest\u001B[39;00m\n\u001B[0;32m    900\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m pytest\u001B[38;5;241m.\u001B[39mraises(expected_exception, match\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m)  \u001B[38;5;66;03m# noqa: PDF010\u001B[39;00m\n\u001B[1;32m--> 903\u001B[0m cython_table \u001B[38;5;241m=\u001B[39m pd\u001B[38;5;241m.\u001B[39mcore\u001B[38;5;241m.\u001B[39mcommon\u001B[38;5;241m.\u001B[39m_cython_table\u001B[38;5;241m.\u001B[39mitems()\n\u001B[0;32m    906\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_cython_table_params\u001B[39m(ndframe, func_names_and_expected):\n\u001B[0;32m    907\u001B[0m \u001B[38;5;250m    \u001B[39m\u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m    908\u001B[0m \u001B[38;5;124;03m    Combine frame, functions from com._cython_table\u001B[39;00m\n\u001B[0;32m    909\u001B[0m \u001B[38;5;124;03m    keys and expected result.\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    921\u001B[0m \u001B[38;5;124;03m        List of three items (DataFrame, function, expected result)\u001B[39;00m\n\u001B[0;32m    922\u001B[0m \u001B[38;5;124;03m    \"\"\"\u001B[39;00m\n",
      "\u001B[1;31mAttributeError\u001B[0m: partially initialized module 'pandas' has no attribute 'core' (most likely due to a circular import)"
     ]
    }
   ],
   "source": [
    "example_selector = MaxMarginalRelevanceExampleSelector.from_examples(\n",
    "    examples,\n",
    "    # 使用OpenAI的模型\n",
    "    OpenAIEmbeddings(openai_api_key=api_key, openai_api_base=api_base),\n",
    "    # 使用FAISS检索\n",
    "    FAISS,\n",
    "    # 结果条数\n",
    "    k = 2,\n",
    ")\n",
    "\n",
    "# 使用小样本模板\n",
    "mmr_prompt = FewShotPromptTemplate(\n",
    "    example_selector = example_selector,\n",
    "    example_prompt = example_prompt,\n",
    "    prefix = \"给出每个输入此的反义词\",\n",
    "    suffix = \"原词:{adjective}\\n反义词:\",\n",
    "    input_variables = [\"adjective\"]\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:57.859930200Z",
     "start_time": "2024-05-07T04:09:57.705927300Z"
    }
   },
   "id": "2a283a1656d55f2e"
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'mmr_prompt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[17], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m# 当输入一个描述情绪的词语时，应该选择同样描述情绪的示例 (但是兼顾最相关和不相关)\u001B[39;00m\n\u001B[1;32m----> 2\u001B[0m \u001B[38;5;28mprint\u001B[39m(mmr_prompt\u001B[38;5;241m.\u001B[39mformat(adjective\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mworry\u001B[39m\u001B[38;5;124m\"\u001B[39m))\n",
      "\u001B[1;31mNameError\u001B[0m: name 'mmr_prompt' is not defined"
     ]
    }
   ],
   "source": [
    "# 当输入一个描述情绪的词语时，应该选择同样描述情绪的示例 (但是兼顾最相关和不相关)\n",
    "print(mmr_prompt.format(adjective=\"worry\"))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:59.271362800Z",
     "start_time": "2024-05-07T04:09:59.252050Z"
    }
   },
   "id": "4b295153af027360"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "ae51cf053d8e768a"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 根据输入响度选择示例(最大余弦相似度)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "510cdcfdb53ecab"
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "from langchain.prompts.example_selector import SemanticSimilarityExampleSelector\n",
    "from langchain.vectorstores import Chroma\n",
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "from langchain.prompts import FewShotPromptTemplate, PromptTemplate\n",
    "\n",
    "import os \n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-zk2f3fc211ff0ad224ecd79efe339d0861d66379d4d11320\"\n",
    "os.environ[\"OPENAI_PROXY\"] = \"https://flag.smarttrot.com/v1/\"\n",
    "\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "# 假设已经有非常多的提示词示例\n",
    "examples = [\n",
    "    {\"input\":\"happy\", \"output\":\"sad\"},\n",
    "    {\"input\":\"tall\", \"output\":\"short\"},\n",
    "    {\"input\":\"sunny\", \"output\":\"gloom\"},\n",
    "    {\"input\":\"windy\", \"output\":\"calm\"},\n",
    "    {\"input\":\"高兴\", \"output\":\"悲伤\"}\n",
    "]\n",
    "\n",
    "\n",
    "# 构造提示词模板\n",
    "example_prompt = PromptTemplate(\n",
    "    input_variables = [\"input\", \"output\"],\n",
    "    template = \"原词: {input}\\n反义词: {output}\"\n",
    ")\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:23.028279900Z",
     "start_time": "2024-05-07T04:09:23.019114400Z"
    }
   },
   "id": "f5d2316c318eaba4"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "# !pip install chromadb==0.4.15"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:23.727703700Z",
     "start_time": "2024-05-07T04:09:23.721706400Z"
    }
   },
   "id": "855b1c1c24e65bdf"
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "partially initialized module 'pandas' has no attribute 'core' (most likely due to a circular import)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[9], line 4\u001B[0m\n\u001B[0;32m      1\u001B[0m example_selector \u001B[38;5;241m=\u001B[39m SemanticSimilarityExampleSelector\u001B[38;5;241m.\u001B[39mfrom_examples(\n\u001B[0;32m      2\u001B[0m     examples,\n\u001B[0;32m      3\u001B[0m     \u001B[38;5;66;03m# 使用OpenAI的模型\u001B[39;00m\n\u001B[1;32m----> 4\u001B[0m     OpenAIEmbeddings(openai_api_key\u001B[38;5;241m=\u001B[39mapi_key, openai_api_base\u001B[38;5;241m=\u001B[39mapi_base),\n\u001B[0;32m      5\u001B[0m     \u001B[38;5;66;03m# 使用FAISS检索\u001B[39;00m\n\u001B[0;32m      6\u001B[0m     Chroma,\n\u001B[0;32m      7\u001B[0m     \u001B[38;5;66;03m# 结果条数\u001B[39;00m\n\u001B[0;32m      8\u001B[0m     k \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m2\u001B[39m,\n\u001B[0;32m      9\u001B[0m )\n\u001B[0;32m     11\u001B[0m \u001B[38;5;66;03m# 使用小样本模板\u001B[39;00m\n\u001B[0;32m     12\u001B[0m similar_prompt \u001B[38;5;241m=\u001B[39m FewShotPromptTemplate(\n\u001B[0;32m     13\u001B[0m     example_selector \u001B[38;5;241m=\u001B[39m example_selector,\n\u001B[0;32m     14\u001B[0m     example_prompt \u001B[38;5;241m=\u001B[39m example_prompt,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m     17\u001B[0m     input_variables \u001B[38;5;241m=\u001B[39m [\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124madjective\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[0;32m     18\u001B[0m )\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\pydantic\\main.py:339\u001B[0m, in \u001B[0;36mpydantic.main.BaseModel.__init__\u001B[1;34m()\u001B[0m\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\pydantic\\main.py:1102\u001B[0m, in \u001B[0;36mpydantic.main.validate_model\u001B[1;34m()\u001B[0m\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\langchain\\embeddings\\openai.py:177\u001B[0m, in \u001B[0;36mOpenAIEmbeddings.validate_environment\u001B[1;34m(cls, values)\u001B[0m\n\u001B[0;32m    170\u001B[0m values[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mopenai_organization\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m get_from_dict_or_env(\n\u001B[0;32m    171\u001B[0m     values,\n\u001B[0;32m    172\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mopenai_organization\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m    173\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mOPENAI_ORGANIZATION\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m    174\u001B[0m     default\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m    175\u001B[0m )\n\u001B[0;32m    176\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[1;32m--> 177\u001B[0m     \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\n\u001B[0;32m    179\u001B[0m     values[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mclient\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m openai\u001B[38;5;241m.\u001B[39mEmbedding\n\u001B[0;32m    180\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mImportError\u001B[39;00m:\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\openai\\__init__.py:9\u001B[0m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mcontextvars\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m ContextVar\n\u001B[0;32m      7\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mtyping\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Optional, TYPE_CHECKING\n\u001B[1;32m----> 9\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m     10\u001B[0m     Audio,\n\u001B[0;32m     11\u001B[0m     ChatCompletion,\n\u001B[0;32m     12\u001B[0m     Completion,\n\u001B[0;32m     13\u001B[0m     Customer,\n\u001B[0;32m     14\u001B[0m     Edit,\n\u001B[0;32m     15\u001B[0m     Deployment,\n\u001B[0;32m     16\u001B[0m     Embedding,\n\u001B[0;32m     17\u001B[0m     Engine,\n\u001B[0;32m     18\u001B[0m     ErrorObject,\n\u001B[0;32m     19\u001B[0m     File,\n\u001B[0;32m     20\u001B[0m     FineTune,\n\u001B[0;32m     21\u001B[0m     Image,\n\u001B[0;32m     22\u001B[0m     Model,\n\u001B[0;32m     23\u001B[0m     Moderation,\n\u001B[0;32m     24\u001B[0m )\n\u001B[0;32m     25\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01merror\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m APIError, InvalidRequestError, OpenAIError\n\u001B[0;32m     27\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m TYPE_CHECKING:\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\openai\\api_resources\\__init__.py:7\u001B[0m\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mdeployment\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Deployment  \u001B[38;5;66;03m# noqa: F401\u001B[39;00m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01medit\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Edit  \u001B[38;5;66;03m# noqa: F401\u001B[39;00m\n\u001B[1;32m----> 7\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01membedding\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Embedding  \u001B[38;5;66;03m# noqa: F401\u001B[39;00m\n\u001B[0;32m      8\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mengine\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Engine  \u001B[38;5;66;03m# noqa: F401\u001B[39;00m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01merror_object\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m ErrorObject  \u001B[38;5;66;03m# noqa: F401\u001B[39;00m\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\openai\\api_resources\\embedding.py:7\u001B[0m\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m util\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi_resources\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mabstract\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mengine_api_resource\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m EngineAPIResource\n\u001B[1;32m----> 7\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mdatalib\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m numpy \u001B[38;5;28;01mas\u001B[39;00m np, assert_has_numpy\n\u001B[0;32m      8\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mopenai\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01merror\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m TryAgain\n\u001B[0;32m     11\u001B[0m \u001B[38;5;28;01mclass\u001B[39;00m \u001B[38;5;21;01mEmbedding\u001B[39;00m(EngineAPIResource):\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\openai\\datalib.py:22\u001B[0m\n\u001B[0;32m     19\u001B[0m     numpy \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[0;32m     21\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[1;32m---> 22\u001B[0m     \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m\n\u001B[0;32m     23\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mImportError\u001B[39;00m:\n\u001B[0;32m     24\u001B[0m     pandas \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\pandas\\__init__.py:138\u001B[0m\n\u001B[0;32m    120\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mcore\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mreshape\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m    121\u001B[0m     concat,\n\u001B[0;32m    122\u001B[0m     lreshape,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    134\u001B[0m     qcut,\n\u001B[0;32m    135\u001B[0m )\n\u001B[0;32m    137\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m api, arrays, errors, io, plotting, tseries\n\u001B[1;32m--> 138\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m testing  \u001B[38;5;66;03m# noqa:PDF015\u001B[39;00m\n\u001B[0;32m    139\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mutil\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m_print_versions\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m show_versions\n\u001B[0;32m    141\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mio\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m    142\u001B[0m     \u001B[38;5;66;03m# excel\u001B[39;00m\n\u001B[0;32m    143\u001B[0m     ExcelFile,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    171\u001B[0m     read_spss,\n\u001B[0;32m    172\u001B[0m )\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\pandas\\testing.py:6\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;124;03mPublic testing utility functions.\u001B[39;00m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m----> 6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m_testing\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m      7\u001B[0m     assert_extension_array_equal,\n\u001B[0;32m      8\u001B[0m     assert_frame_equal,\n\u001B[0;32m      9\u001B[0m     assert_index_equal,\n\u001B[0;32m     10\u001B[0m     assert_series_equal,\n\u001B[0;32m     11\u001B[0m )\n\u001B[0;32m     13\u001B[0m __all__ \u001B[38;5;241m=\u001B[39m [\n\u001B[0;32m     14\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124massert_extension_array_equal\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m     15\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124massert_frame_equal\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m     16\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124massert_series_equal\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m     17\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124massert_index_equal\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m     18\u001B[0m ]\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\pandas\\_testing\\__init__.py:903\u001B[0m\n\u001B[0;32m    898\u001B[0m     \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mpytest\u001B[39;00m\n\u001B[0;32m    900\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m pytest\u001B[38;5;241m.\u001B[39mraises(expected_exception, match\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m)  \u001B[38;5;66;03m# noqa: PDF010\u001B[39;00m\n\u001B[1;32m--> 903\u001B[0m cython_table \u001B[38;5;241m=\u001B[39m pd\u001B[38;5;241m.\u001B[39mcore\u001B[38;5;241m.\u001B[39mcommon\u001B[38;5;241m.\u001B[39m_cython_table\u001B[38;5;241m.\u001B[39mitems()\n\u001B[0;32m    906\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_cython_table_params\u001B[39m(ndframe, func_names_and_expected):\n\u001B[0;32m    907\u001B[0m \u001B[38;5;250m    \u001B[39m\u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m    908\u001B[0m \u001B[38;5;124;03m    Combine frame, functions from com._cython_table\u001B[39;00m\n\u001B[0;32m    909\u001B[0m \u001B[38;5;124;03m    keys and expected result.\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    921\u001B[0m \u001B[38;5;124;03m        List of three items (DataFrame, function, expected result)\u001B[39;00m\n\u001B[0;32m    922\u001B[0m \u001B[38;5;124;03m    \"\"\"\u001B[39;00m\n",
      "\u001B[1;31mAttributeError\u001B[0m: partially initialized module 'pandas' has no attribute 'core' (most likely due to a circular import)"
     ]
    }
   ],
   "source": [
    "example_selector = SemanticSimilarityExampleSelector.from_examples(\n",
    "    examples,\n",
    "    # 使用OpenAI的模型\n",
    "    OpenAIEmbeddings(openai_api_key=api_key, openai_api_base=api_base),\n",
    "    # 使用FAISS检索\n",
    "    Chroma,\n",
    "    # 结果条数\n",
    "    k = 2,\n",
    ")\n",
    "\n",
    "# 使用小样本模板\n",
    "similar_prompt = FewShotPromptTemplate(\n",
    "    example_selector = example_selector,\n",
    "    example_prompt = example_prompt,\n",
    "    prefix = \"给出每个输入此的反义词\",\n",
    "    suffix = \"原词:{adjective}\\n反义词:\",\n",
    "    input_variables = [\"adjective\"]\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:24.271011100Z",
     "start_time": "2024-05-07T04:09:24.106843Z"
    }
   },
   "id": "711ba02ee730122d"
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'similar_prompt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[10], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m# 当输入一个描述情绪的词语时，应该选择同样描述情绪的示例 \u001B[39;00m\n\u001B[1;32m----> 2\u001B[0m \u001B[38;5;28mprint\u001B[39m(similar_prompt\u001B[38;5;241m.\u001B[39mformat(adjective\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mworry\u001B[39m\u001B[38;5;124m\"\u001B[39m))\n",
      "\u001B[1;31mNameError\u001B[0m: name 'similar_prompt' is not defined"
     ]
    }
   ],
   "source": [
    "# 当输入一个描述情绪的词语时，应该选择同样描述情绪的示例 \n",
    "print(similar_prompt.format(adjective=\"worry\"))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T04:09:24.927637600Z",
     "start_time": "2024-05-07T04:09:24.916087200Z"
    }
   },
   "id": "2c8fd78a85ec4454"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "e19babdbadd3cd65"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
